package com.ecommerce.controller;

import com.ecommerce.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/banners")
@CrossOrigin(origins = "*")
public class BannerController {
    
    @Autowired
    private BannerService bannerService;
    
    /**
     * 获取所有激活的轮播图
     */
    @GetMapping
    public ResponseEntity<?> getActiveBanners() {
        try {
            var banners = bannerService.getActiveBanners();
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", banners);
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
    
    /**
     * 获取主轮播图
     */
    @GetMapping("/main")
    public ResponseEntity<?> getMainBanners() {
        try {
            var banners = bannerService.getMainBanners();
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", banners);
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
} 